package com.hzyc.springdata.jpa.dao;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.hzyc.springdata.jpa.domain.User;

@Repository
public interface UserJpaRepository 
	extends JpaRepository<User,Long>{

	public List<User> findByUserNameAndPassWord(String userName,String password);
	
	public List<User> findByRoleOrderByIdDesc(String role);
	
	public Long countByUserName(String name);
	
	/**
	 * 查询并分页
	 * @param role
	 * @param pageable
	 * @return
	 */
	public Page<User> findByRole(String role, Pageable pageable);
	
	/**
	 * 排序
	 * @param role
	 * @param sort
	 * @return
	 */
	public List<User> findByRole(String role,Sort sort);
	
	/**
	 * 获取第一条数据
	 * @param userName
	 * @return
	 */
	public User findFirstByUserName(String userName);
	
	/**
	 * 获取前10条数据
	 * @param userName
	 * @return
	 */
	public List<User>  findTop10ByUserName(String userName);
	
	/**
	 * Query注解查询 User为实体类，不能直接使用表名称，
	 * @param userName
	 * @return
	 */
	@Query(value="select u from User u where u.userName=?1")
	public List<User> queryUser(String userName);
	
	
	/**
	 * Query注解查询 User为实体类，不能直接使用表名称，
	 * @param userName
	 * @return
	 */
	@Query(value="select u from User u where u.userName=:userName and password=:pwd")
	public User login(@Param("userName")String userName, @Param("pwd")String password);

	@Modifying
	@Transactional
	@Query(value="update User u set u.userName=?1 where u.id=?2")
	public int setUserNameById(String userName, long id);
}
